package com.mail.server.controller;


import com.mail.model.db.PmsProductAttribute;
import com.mail.model.vo.PageResult;
import com.mail.server.common.R;
import com.mail.server.service.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author WJZ
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService productAttributeService;

    /**
     * 根据商品分类的id获取商品属性及属性分类
     * @param productCategoryId
     * @return
     */
    @GetMapping("/attrInfo/{productCategoryId}")
    public R queryProductAttributeAndCategory(@PathVariable Integer productCategoryId){
        List<Map<String,Integer>> mapList = productAttributeService.queryProductAttributeAndCategory(productCategoryId);
        return R.success(mapList);
    }

    /**
     * 添加商品属性信息
     * @return
     */
    @PostMapping("/create")
    public R createProductAttribute(@RequestBody PmsProductAttribute productAttribute){
        productAttributeService.createProductAttribute(productAttribute);
        return R.success(null);
    }

    /**
     * 批量删除商品属性
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public R deleteAttributeByIds(Map map){
        List<Integer> ids = (List<Integer>) map.get("ids");
        productAttributeService.deleteAttributeByIds(ids);
        return R.success(null);
    }

    /**
     * 根据分类查询属性列表或参数列表
     * @param cid
     * @param type  0:属性  1:参数
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{cid}")
    public R queryAttributeList(@PathVariable Integer cid,Integer type,
                                @RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult pageResult = productAttributeService.queryAttributeList(cid,type,pageNum,pageSize);
        return R.success(pageResult);
    }

    /**
     * 修改商品属性信息
     * @param id
     * @param productAttributeParam
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateProductAttribute(@PathVariable Integer id,@RequestBody PmsProductAttribute productAttributeParam){
        productAttributeService.updateProductAttribute(Long.valueOf(id),productAttributeParam);
        return R.success(null);
    }

    /**
     * 查询单个商品属性
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R queryProductAttributeById(@PathVariable Integer id){
        PmsProductAttribute productAttribute = productAttributeService.queryProductAttributeById(Long.valueOf(id));
        return R.success(productAttribute);
    }
}
